package threadTask;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * 说明:任务线程池管理器
 * Created on 2023/9/21 15:00
 *
 * @author yanghongjun <1737619387@qq.com>
 */
public class ThreadPoolManager {


    /**
     * 用枚举来实现单例
     */
    private enum Singleton {
        INSTANCE;
        ThreadPoolManager processor;

        Singleton() {
            this.processor = new ThreadPoolManager();
        }

        ThreadPoolManager getProcessor() {
            return processor;
        }
    }

    /**
     * 获取实例对象
     *
     * @return
     */
    public static ThreadPoolManager getInstance() {
        return ThreadPoolManager.Singleton.INSTANCE.getProcessor();
    }


    // 线程池
    private final ExecutorService fixedThreadPool;


    public ThreadPoolManager() {
        fixedThreadPool = new ThreadPoolExecutor(
                Runtime.getRuntime().availableProcessors(),//核心线程数
                200,//最大线程数
                0L,//线程空闲时间
                TimeUnit.MILLISECONDS,//时间单位
                new LinkedBlockingQueue<Runnable>());////任务队列
    }


    public void addTask(ThreadTask threadTask) {
        fixedThreadPool.execute(threadTask);
    }

}
